package antlr4.definitive.d3_arrayinit.d3_1_arrayinit.run;

import antlr4.definitive.d3_arrayinit.d3_1_arrayinit.gen.ArrayInitLexer;
import antlr4.definitive.d3_arrayinit.d3_1_arrayinit.gen.ArrayInitParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.util.Scanner;


/**
 * 根据数组声明程序，添加一个翻译器，在合适的位置把原来数组中的数字，转为unicode字符串
 */
public class Translate {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个 数组初始化格式的串 如：{1,2,3}");
        String input = scanner.nextLine();
        CodePointCharStream codePointCharStream = CharStreams.fromString(input);
        //新建一个词法分析器，处理输入的CharStream
        ArrayInitLexer lexer = new ArrayInitLexer(codePointCharStream);
        //新建一个词法符号的缓冲区，用于存储词法分析器将生成的词法符号
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        //新建一个语法分析器，处理词法符号缓冲区中的内容
        ArrayInitParser parser = new ArrayInitParser(tokens);
        ParseTree tree = parser.init();//针对init规则，开始语法分析
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(new ShortToUnicodeString(), tree);
        System.out.println();//翻译完成后，打印一个\n
    }
}
